def find_operand(expression, c_index):
    # find the left number of the operator c
    left_index = c_index - 1
    while True:
        if expression[left_index] in ['.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
            left_index -= 1
        else:
            break
        if left_index == -1:
            break

    # find the right number of the operator c
    right_index = c_index + 1
    while True:
        if expression[right_index] in ['.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
            right_index += 1
        else:
            break
        if right_index == len(expression):
            break
    return left_index, right_index


def calculate_express(expression):
    flag = True
    for c in expression:
        if c not in ['.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
            flag = False
    if flag:
        return expression

    # find the innermost pair of parentheses
    left_bracket_index = None
    right_bracket_index = None
    c_index = 0
    for c in expression:
        if c == '(':
            left_bracket_index = c_index
        elif c == ')':
            right_bracket_index = c_index
            break
        else:
            pass
        c_index += 1

    # first calculate the expression in the innermost pair of parentheses
    if left_bracket_index != None and right_bracket_index != None:
        new_expression = calculate_express(expression[left_bracket_index + 1:right_bracket_index])
        if left_bracket_index>0:
            new_expression = expression[:left_bracket_index] + new_expression
        if right_bracket_index<len(expression)-1:
            new_expression = new_expression + expression[right_bracket_index+1:]
        print(new_expression)
        return calculate_express(new_expression)

    # then calculate multiply and divide from left to right
    c_index = 0
    for c in expression:
        if c == '*' or c == '/':
            left_index, right_index = find_operand(expression, c_index)
            left_number = expression[left_index + 1:c_index]
            right_number = expression[c_index + 1:right_index]
            if c == '*':
                result = eval(left_number) * eval(right_number)
            else:
                result = eval(left_number) / eval(right_number)
            new_expression = expression[:left_index + 1] + str(result) + expression[right_index:]
            print(new_expression)
            return calculate_express(new_expression)
        c_index += 1

    c_index = 0
    for c in expression:
        if c == '+' or c == '-':
            left_index, right_index = find_operand(expression, c_index)
            left_number = expression[left_index + 1:c_index]
            right_number = expression[c_index + 1:right_index]
            if c == '+':
                result = eval(left_number) + eval(right_number)
            else:
                result = eval(left_number) - eval(right_number)
            new_expression = expression[:left_index + 1] + str(result) + expression[right_index:]
            print(new_expression)
            return calculate_express(new_expression)
        c_index += 1


if __name__ == '__main__':
    # calculate_express('19*2-6/3')
    # calculate_express('(19*2-6/3)')
    calculate_express('(((3*(2+5)-2)*2-6/3))')
